package System_code.Singleton;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-08-03
 * Time: 15:37
 *实现单例模式的两种方式：饿汉模式和懒汉模式
 */
// 单例, 饿汉的方式.
class SingletonHungry{
    private static SingletonHungry instance=new SingletonHungry();
    public static SingletonHungry getInstance(){
        return instance;
    }
    // 构造方法设为私有! 其他的类想来 new 就不行了!!
    private SingletonHungry(){};
}
// 懒汉模式的实现
class SingletonLazy{
    private static SingletonLazy instance=null;
    public static SingletonLazy getInstance(){
        if(instance==null){
            synchronized (SingletonLazy.class){
               if(instance==null){
                   instance=new SingletonLazy();
               }

            }
        }
        return instance;
    }
}
public class Demo17 {
    SingletonHungry instance=SingletonHungry.getInstance();
    SingletonLazy singletonLazy=SingletonLazy.getInstance();

}
